package mlib

import (
	"errors"
	"strings"
)

type MusicManager struct {
	musics []MusicEntry
}

type MusicEntry struct {
	Id     string
	Name   string
	Artist string
	Source string
	Type   string
}

func NewManager() *MusicManager {
	//构建一个音乐管理器
	return &MusicManager{make([]MusicEntry, 0)}
}

//  获取音乐列表大小
func (receiver MusicManager) Len() int {
	return len(receiver.musics)
}

// 获取音乐列表元素
func (receiver MusicManager) Get(index int) (music *MusicEntry, err error) {
	if index >= receiver.Len() || index < 0 {
		return nil, errors.New("Index out of range")
	}
	return &receiver.musics[index], nil
}

// 关键词查找
func (receiver MusicManager) Find(name string) (music *MusicEntry) {
	for _, v := range receiver.musics {
		if strings.Contains(v.Name, name) {
			return &v
		}
	}
	return nil
}

// 添加音乐
func (receiver *MusicManager) Add(music *MusicEntry) {
	receiver.musics = append(receiver.musics, *music)
}

// 删除音乐
func (receiver *MusicManager) Remove(index int) (*MusicEntry, error) {
	if index >= receiver.Len() || index < 0 {
		return nil, errors.New("Index out of range")
	}
	removeEle := &receiver.musics[index]
	receiver.musics = append(receiver.musics[:index], receiver.musics[index+1:]...)
	return removeEle, nil
}
